﻿using System;
using System.Net;
using System.Xml.Serialization;
using AdRotator.Common;
#if SILVERLIGHT
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
#endif

namespace AdRotator.Silverlight.AdProviders
{
    public class AdDuplexAd : AdProviderBase
#if SILVERLIGHT
        , IAdProviderNotManipulatingVisualTree
#endif
    {
        private string _appId;

        [XmlAttribute("AppId")]
        /// <summary>
        /// Whether or not this is the default ad provider to show
        /// </summary>
        public string AppId
        {
            get
            {
                return _appId;
            }
            set
            {
                if (_appId != value)
                {
                    _appId = value;
                    NotifyPropertyChanged("AppId");
                }
            }
        }

        public override bool IsValid
        {
            get
            {
                return !String.IsNullOrEmpty(AppId);
            }
        }

#if SILVERLIGHT
        private AdDuplex.AdControl _currentAdControl;
#endif

        public override void Initialize()
        {
#if SILVERLIGHT
            if (_currentAdControl != null)
            {
                _currentAdControl.AdLoaded += new EventHandler<AdDuplex.AdLoadedEventArgs>(_currentAdControl_AdLoaded);
                _currentAdControl.AdLoadingError += new EventHandler<AdDuplex.AdLoadingErrorEventArgs>(_currentAdControl_AdLoadingError);
            }
#endif
        }

        public override void Deinitialize()
        {
#if SILVERLIGHT
            if (_currentAdControl != null)
            {
                _currentAdControl.AdLoaded -= new EventHandler<AdDuplex.AdLoadedEventArgs>(_currentAdControl_AdLoaded);
                _currentAdControl.AdLoadingError -= new EventHandler<AdDuplex.AdLoadingErrorEventArgs>(_currentAdControl_AdLoadingError);
            }
#endif
        }

#if SILVERLIGHT
        public FrameworkElement CreateAd()
        {
            try
            {
                _currentAdControl = new AdDuplex.AdControl();
                _currentAdControl.AppId = AppId;
                _currentAdControl.IsTest = IsTest;
                RaiseLogEvent(String.Format("AdDuplex ad with AppId {0} created with test mode {1}", AppId, IsTest ? "on" : "off"));
                return _currentAdControl;
            }
            catch (Exception ex)
            {
                RaiseLogEvent("Error occurred when creating AdDuplex ad", ex);
                return null;
            }
        }

        private void _currentAdControl_AdLoaded(object sender, AdDuplex.AdLoadedEventArgs e)
        {
            RaiseLogEvent(String.Format("AdDuplex ad successfully loaded with the ad {0} {1}, pointing to marketplace ad {3}", e.NewAd.Line1, e.NewAd.Line2, e.NewAd.MarketplaceAppId));
        }

        private void _currentAdControl_AdLoadingError(object sender, AdDuplex.AdLoadingErrorEventArgs e)
        {
            RaiseLogEvent(String.Format("Error occurred when loading AdDuplex ad", e.Error));
            RaiseAdLoadingErrorEvent();
        }
#endif
    }
}
